import 'package:gold/generated/json/base/json_convert_content.dart';
import 'package:gold/http/http.dart';
import 'package:gold/model/address_model.dart';
import 'package:gold/model/commission_list_model.dart';
import 'package:gold/model/coupon_model.dart';
import 'package:gold/model/customer_model.dart';
import 'package:gold/model/express_company_model.dart';
import 'package:gold/model/fund_list_model.dart';
import 'package:gold/model/gold_banner_model.dart';
import 'package:gold/model/gold_price_model.dart';
import 'package:gold/model/gold_recover_order_model.dart';
import 'package:gold/model/gold_recover_result_model.dart';
import 'package:gold/model/gold_recover_shop_model.dart';
import 'package:gold/model/goods_detail_model.dart';
import 'package:gold/model/goods_list_model.dart';
import 'package:gold/model/list_model.dart';
import 'package:gold/model/order_model.dart';
import 'package:gold/model/point_list_model.dart';
import 'package:gold/model/point_statistics_model.dart';
import 'package:gold/model/points_category_model.dart';
import 'package:gold/model/pool_list_model.dart';
import 'package:gold/model/pool_model.dart';
import 'package:gold/model/recover_cancel_reason_model.dart';
import 'package:gold/model/refund_model.dart';
import 'package:gold/model/review_list_model.dart';
import 'package:gold/model/shop_commission_model.dart';
import 'package:gold/model/shop_model.dart';
import 'package:gold/model/user_list_model.dart';
import 'package:gold/model/user_model.dart';
import 'package:gold/model/withdrawal_record_model.dart';

/// 描述: 网络数据工具类
/// 
/// @Author tangzhentao
///  @CreatedTime 10:57 2022/6/18
///

/// 解析的key
final _success = 'success';
final _code = 'code';
final _msg = 'msg';
final _data = 'data';

/// 数据类解析工厂
class EntityFactory {

  static T? generateOBJ<T>(json) {
    if (json == null || json == '') {
      return null;
    } else {
      final desc = T.toString();
      if (desc == 'String') {
        return json.toString() as T;
      } else if (desc == 'List<int>') {
        return (json as List<dynamic>).map((e) => jsonConvert.convert<int>(e) as int).toList() as T;
      } else if (desc == 'UserModel') {
        return UserModel.fromJson(json) as T;
      } else if (desc == 'GoldPriceModel') {
        return GoldPriceModel.fromJson(json) as T;
      } else if (desc == 'GoldRecoverResultModel') {
        return GoldRecoverResultModel.fromJson(json) as T;
      } else if (desc == 'GoldRecoverOrderModel') {
        return GoldRecoverOrderModel.fromJson(json) as T;
      } else if (desc == 'GoodsDetailModel') {
        return GoodsDetailModel.fromJson(json) as T;
      } else if (desc == 'GoldBannerModel') {
        return GoldBannerModel.fromJson(json) as T;
      } else if (desc == 'PointsCategoryModel') {
        return PointsCategoryModel.fromJson(json) as T;
      } else if (desc == 'PoolModel') {
        return PoolModel.fromJson(json) as T;
      } else if (desc == 'PoolListModel') {
        return PoolListModel.fromJson(json) as T;
      } else if (desc == 'PoolRankModel') {
        return PoolRankModel.fromJson(json) as T;
      } else if (desc == 'RecoverCancelReasonModel') {
        return RecoverCancelReasonModel.fromJson(json) as T;
      } else if (desc == 'PointStatisticsModel') {
        return PointStatisticsModel.fromJson(json) as T;
      } else if (desc == 'FundListModel') {
        return FundListModel.fromJson(json) as T;
      } else if (desc == 'PointListModel') {
        return PointListModel.fromJson(json) as T;
      } else if (desc == 'ReviewListModel') {
        return ReviewListModel.fromJson(json) as T;
      } else if (desc == 'AddressModel') {
        return AddressModel.fromJson(json) as T;
      } else if (desc == 'RefundModel') {
        return RefundModel.fromJson(json) as T;
      } else if (desc == 'UserListModel') {
        return UserListModel.fromJson(json) as T;
      } else if (desc == 'OrderModel') {
        return OrderModel.fromJson(json) as T;
      } else if (desc == 'ShopModel') {
        return ShopModel.fromJson(json) as T;
      } else if (desc == 'ShopCommissionModel') {
        return ShopCommissionModel.fromJson(json) as T;
      } else if (desc == 'CommissionListModel') {
        return CommissionListModel.fromJson(json) as T;
      } else if (desc == 'ExpressCompanyModel') {
        return ExpressCompanyModel.fromJson(json) as T;
      } else if (desc.startsWith('ListModel')) {
        final data = desc.substring(10, desc.length - 1);
        if (data.contains('String')) {
          return ListModel<String>.fromJson(json) as T;
        } if (data.contains('AddressModel')) {
          return ListModel<AddressModel>.fromJson(json) as T;
        } if (data.contains('GoldRecoverShopModel')) {
          return ListModel<GoldRecoverShopModel>.fromJson(json) as T;
        } if (data.contains('GoldRecoverOrderModel')) {
          return ListModel<GoldRecoverOrderModel>.fromJson(json) as T;
        } if (data.contains('GoodsListModel')) {
          return ListModel<GoodsListModel>.fromJson(json) as T;
        } if (data.contains('OrderModel')) {
          return ListModel<OrderModel>.fromJson(json) as T;
        } if (data.contains('WithdrawalRecordModel')) {
          return ListModel<WithdrawalRecordModel>.fromJson(json) as T;
        } if (data.contains('CustomerModel')) {
          return ListModel<CustomerModel>.fromJson(json) as T;
        } if (data.contains('CouponModel')) {
          return ListModel<CouponModel>.fromJson(json) as T;
        } else {
          return ListModel<dynamic>.fromJson(json) as T;
        }
      } else {
        return json as T;
      }
    }
  }
}

/// object数据类
class Entity<T> {
  bool success;
  int code;
  String msg;
  T? data;

  Entity({this.success = false, this.code = HttpErrorType.unKnow, this.msg = '未知错误', this.data});

  factory Entity.fromJson(json) {
    return Entity(
        success: jsonConvert.convert<bool>(json[_success])?? false,
        code: jsonConvert.convert<int>(json[_code])?? -1,
        msg: jsonConvert.convert<String>(json[_msg])?? '未知错误',
        data: EntityFactory.generateOBJ<T>(json[_data]),
    );
  }

  Map toJson() => {
    _success: success,
    _code: code,
    _msg: msg,
    _data: data,
  };
}

/// List<Object> 数组数据类
class ListEntity<T> {
  bool success;
  int code;
  String msg;
  List<T>? data;

  ListEntity({this.success = false, this.code = HttpErrorType.unKnow, this.msg = '未知错误', this.data});

  factory ListEntity.fromJson(Map<String, dynamic> json) {
    List<T>? mData;
    if (json[_data] != null) {
      mData = <T>[];
      for (var element in (json[_data] as List)) {
        final s = EntityFactory.generateOBJ<T>(element);
        if (s != null) mData.add(s);
      }
    }

    return ListEntity(
        success: jsonConvert.convert<bool>(json[_success])?? false,
        code: jsonConvert.convert<int>(json[_code])?? -1,
        msg: jsonConvert.convert<String>(json[_code])?? '未知错误',
        data: mData
    );
  }
}
